# models.py
from typing import List, Optional, Dict
from datetime import datetime
from dataclasses import dataclass, field

# ================= 数据模型定义 =================
@dataclass
class RawData:
    """原始数据模型（行号索引）"""
    id: int
    well_name: str
    base_level: Optional[int] = 0
    created_at: str = field(default_factory=lambda: datetime.now().isoformat())


@dataclass
class FieldDefinition:
    """字段定义模型"""
    field_id: int
    raw_id: int
    field_name: str
    table_name: str


@dataclass
class FieldSelection:
    """字段选择模型"""
    field_id: int
    field_name: str
    alias: Optional[str] = None
    is_visible: bool = True

    def validate(self):
        """验证字段有效性"""
        if not self.field_name:
            raise ValueError("字段名称不能为空")


@dataclass
class UserSelection:
    """用户选择主记录模型"""
    selection_id: Optional[int]
    raw_id: int
    start_row: int
    end_row: int
    selected_at: Optional[str]
    fields: List[FieldSelection]

    def __post_init__(self):
        """初始化验证"""
        if self.fields is None:
            self.fields = []
        self.validate()

    def validate(self):
        """验证用户选择数据有效性"""
        if self.start_row < 0:
            raise ValueError("起始行号不能为负数")
        if self.end_row < self.start_row:
            raise ValueError("结束行号必须大于等于起始行号")